//
//  MuDiDiViewController.m
//  TravelYo
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 TravelYo. All rights reserved.
//

#import "MuDiDiViewController.h"

#import "TYDataManager.h"

#import "MDDZhouModel.h"

#import "UIImageView+WebCache.h"

#import "MuDiDiTableViewCell.h"

#import "UIView+WLFrame.h"

#import "GuoJiaViewController.h"


// 宽高
#define Kheight [UIScreen mainScreen].bounds.size.height
#define Kwidth [UIScreen mainScreen].bounds.size.width

@interface MuDiDiViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate>

#pragma mark -- 解析数据属性
// 大数组
@property (nonatomic, strong) NSMutableArray *dataArray;
// 解析后的洲字典包含（洲名，国家名， 热门国家名，洲英文文明）
@property (nonatomic, strong) NSMutableDictionary *zhouArray;
#pragma mark -- 布局属性
@property (nonatomic, strong) UIImageView *imageView;
// 标题标题
@property (nonatomic, strong) UILabel *lable;
// 标题标题
@property (nonatomic, strong) UILabel *biaoWei;
// 集合视图
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@property (nonatomic, strong) UICollectionView *collectionView;
// 集合视图的cell
@property (nonatomic, strong) UICollectionViewCell *cell;
// 表头
@property (nonatomic, strong) UICollectionReusableView *tou;
// 表尾
@property (nonatomic, strong) UICollectionReusableView *wei;
// uitableView
@property (nonatomic, strong) UITableView *tableView;
// 国家ID(记录国家ID,实现跳转的网址解析)
@property (nonatomic, strong) NSString * guoJiaID;

@end

@implementation MuDiDiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"目的地";
    self.navigationController.navigationBar.translucent = YES;
   // self.navigationController.navigationBar.backgroundColor = kMainColor;
    [self jieXi:0];
    
    [self biaotou];
    
    [self addUICollectionView];
    
    [self addUITableView];
    
    [self addUITableView];

}

/// 解析数据 i判断解析那个大洲
- (void)jieXi:(NSInteger)i
{
    [[TYDataManager shareManager] requestDataWithUrl:@"http://open.qyer.com/qyer/footprint/continent_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=40.02935814101822&lon=116.337349642413&page=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=3D292DEC-1F82-4FE7-9AE7-A1866F5F24E7&track_os=ios%25209.0.2&v=1" block:^(NSData *data){
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        
        NSArray *array = [dic valueForKey:@"data"];
        
        _dataArray = [NSMutableArray array];
        
        for (NSDictionary *dic in array) {
            MDDZhouModel *model = [[MDDZhouModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:model];
        }
        _zhouArray = _dataArray[i];
        // 刷新数据
        [_tableView reloadData];
        [_collectionView reloadData];
        
    }];
}

// 表头布局
- (void)biaotou
{
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, Kwidth, 240)];
    _imageView.backgroundColor = [UIColor redColor];
    _imageView.image = [UIImage imageNamed:@"123"];
    
    _lable = [[UILabel alloc] initWithFrame:CGRectMake(5, 260, 200, 40)];
    _lable.text = @"亚洲热门目的地";
    _lable.font = [UIFont systemFontOfSize:18];
    
    _biaoWei = [[UILabel alloc] initWithFrame:CGRectMake(17, 0, 200, 30)];
    //    _biaoWei.backgroundColor = [UIColor redColor];
    _biaoWei.text = @"亚洲其他目的地";
    _biaoWei.font = [UIFont systemFontOfSize:18];
    
    
    
    // 打开交互
    _imageView.userInteractionEnabled = YES;
    
    UIButton *yaZhous = [UIButton buttonWithType:UIButtonTypeCustom];
    yaZhous.frame = CGRectMake(_imageView.width *0.7, _imageView.height *0.21, 50, 30);
    //    [yaZhous setTitle:@"亚洲" forState:UIControlStateNormal];
    //    yaZhous.backgroundColor = [UIColor redColor];
    yaZhous.titleLabel.font = [UIFont systemFontOfSize:15];
    [yaZhous addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    yaZhous.tag = 0;
    [_imageView addSubview:yaZhous];
    
    
    UIButton *ouZhous = [UIButton buttonWithType:UIButtonTypeCustom];
    ouZhous.frame = CGRectMake(_imageView.width * 0.52, _imageView.height *0.17, 50, 30);
    //    [ouZhous setTitle:@"欧洲" forState:UIControlStateNormal];
    //    ouZhous.backgroundColor = [UIColor redColor];
    ouZhous.tag = 1;
    ouZhous.titleLabel.font = [UIFont systemFontOfSize:15];
    [ouZhous addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [_imageView addSubview:ouZhous];
    
    UIButton *beiMeiZhous = [UIButton buttonWithType:UIButtonTypeCustom];
    beiMeiZhous.frame = CGRectMake(Kwidth / 10, _imageView.height / 6, 60, 30);
    //    [beiMeiZhous setTitle:@"北美洲" forState:UIControlStateNormal];
    //    beiMeiZhous.backgroundColor = [UIColor redColor];
    beiMeiZhous.titleLabel.font = [UIFont systemFontOfSize:15];
    beiMeiZhous.tag = 2;
    [beiMeiZhous addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [_imageView addSubview:beiMeiZhous];
    
    UIButton *nanMeiZhous = [UIButton buttonWithType:UIButtonTypeCustom];
    nanMeiZhous.frame = CGRectMake(_imageView.width *0.23, _imageView.height *0.53, 60, 30);
    //    [nanMeiZhous setTitle:@"南美洲" forState:UIControlStateNormal];
    //    nanMeiZhous.backgroundColor = [UIColor redColor];
    nanMeiZhous.tag = 3;
    nanMeiZhous.titleLabel.font = [UIFont systemFontOfSize:15];
    [nanMeiZhous addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [_imageView addSubview:nanMeiZhous];
    
    UIButton *daYangZhous = [UIButton buttonWithType:UIButtonTypeCustom];
    daYangZhous.frame = CGRectMake(_imageView.width * 0.75, _imageView.height * 0.51, 60, 30);
    //    [daYangZhous setTitle:@"大洋洲" forState:UIControlStateNormal];
    //    daYangZhous.backgroundColor = [UIColor redColor];
    daYangZhous.tag = 4;
    daYangZhous.titleLabel.font = [UIFont systemFontOfSize:15];
    [daYangZhous addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [_imageView addSubview:daYangZhous];
    
    UIButton *feiZhous = [UIButton buttonWithType:UIButtonTypeCustom];
    feiZhous.frame = CGRectMake(_imageView.width * 0.47, _imageView.height * 0.5, 50, 30);
    //    [feiZhous setTitle:@"非洲" forState:UIControlStateNormal];
    //    feiZhous.backgroundColor = [UIColor redColor];
    feiZhous.tag = 5;
    feiZhous.titleLabel.font = [UIFont systemFontOfSize:15];
    [feiZhous addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [_imageView addSubview:feiZhous];
    
    UIButton *nanJiZhous = [UIButton buttonWithType:UIButtonTypeCustom];
    nanJiZhous.frame = CGRectMake(_imageView.width * 0.6, _imageView.height * 0.71, 60, 30);
    //    [nanJiZhous setTitle:@"南极洲" forState:UIControlStateNormal];
    //    nanJiZhous.backgroundColor = [UIColor redColor];
    nanJiZhous.tag = 6;
    nanJiZhous.titleLabel.font = [UIFont systemFontOfSize:15];
    [nanJiZhous addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [_imageView addSubview:nanJiZhous];
    
    
}

// 按钮点击方法
- (void)action:(UIButton *)button
{
    // 重点 （移除图片上的UILable 避免重叠）
    //    UIImageView *imageView = (UIImageView *)[self.view viewWithTag:100];
    //    imageView.backgroundColor = [UIColor greenColor];
    //    [imageView removeFromSuperview];
    
    if (button.tag == 0) {
        //        [_cell removeFromSuperview];
        [self jieXi:0];
        _lable.text = @"亚洲热门目的地";
        _biaoWei.text = @"亚洲其他目的地";
    } else if (button.tag == 1){
        //        [_cell removeFromSuperview];
        [self jieXi:1];
        
        _lable.text = @"欧洲热门目的地";
        _biaoWei.text = @"欧洲其他目的地";
    } else if (button.tag == 2){
        //        [_cell removeFromSuperview];
        _lable.text = @"北美洲热门目的地";
        _biaoWei.text = @"北美洲其他目的地";
        [self jieXi:2];
    } else if (button.tag == 3){
        _lable.text = @"南美洲热门目的地";
        _biaoWei.text = @"南美洲其他目的地";
        [self jieXi:3];
        
    } else if (button.tag == 4){
        _lable.text = @"大洋洲热门目的地";
        [self jieXi:4];
        
    } else if (button.tag == 5){
        _lable.text = @"非洲热门目的地";
        _biaoWei.text = @"非洲其他目的地";
        [self jieXi:5];
        
    } else if (button.tag == 6){
        _lable.text = @"南极洲热门目的地";
        _biaoWei.text = @"南极洲其他目的地";
        [self jieXi:6];
        
    }
    
}

// 集合视图
- (void)addUICollectionView
{
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.minimumLineSpacing = 10;
    _layout.minimumInteritemSpacing = 0;
    _layout.itemSize = CGSizeMake(Kwidth / 2 -10, 250);
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    // 设置表头
    _layout.headerReferenceSize = CGSizeMake(0, 300);
    
    // 设置表尾 (表尾在tealeView里设置了)
    //    _layout.footerReferenceSize = CGSizeMake(0, 0);
    //    _layout.footerReferenceSize = CGSizeMake(0, 0);
    // 表头表尾 只有高度有效果
    
    // 设置内边距
    _layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    
    // 初始化 集合视图
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 44, Kwidth, 1000) collectionViewLayout:_layout];
    
    // 设置代理
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    // collectionView默认是黑的
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    // 添加视图
    [self.view addSubview:_collectionView];
    
    
    // 注册 你要用的cell
    // Identifier 重用标识符 要一致
    // Class你的cell是那个类 就填那个类
    // 使用系统的就注册系统的 如果自定义的话 就注册自定义的
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
    
    // 注册表头
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader"];
    // 注册表尾
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFoot"];
}

#pragma mark -- 集合视图代理方法 --

// 返回每个分区的Item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *array = [self.zhouArray valueForKey:@"hot_country"];
    return array.count;
}

// 返回每个item的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 这个方法里面 包括了 咱们之前创建tableViewCell写的一堆
    // 必须有一步 必须要有 注册cell
    _cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    
    // 系统没有像tableView一样 给咱们提供布局方式
    // 咱们要使用UICollectionViewCell 一般都是自定义再使用 跟tableView一样 所有的自定义控件都要加在contentView上面
    _cell.contentView.backgroundColor = [UIColor redColor];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _layout.itemSize.width, _layout.itemSize.height)];
    
    NSArray *array = [self.zhouArray valueForKey:@"hot_country"];
    
    NSString *string = [array[indexPath.row] valueForKey:@"photo"];
    
    [image sd_setImageWithURL:[NSURL URLWithString:string]];
    
    [_cell.contentView addSubview:image];
    
#pragma mark -- cell上的文字
    
    UILabel *guoJia = [[UILabel alloc] initWithFrame:CGRectMake(10, _layout.itemSize.height - 80, 100, 50)];
    guoJia.textColor = [UIColor whiteColor];
    guoJia.backgroundColor = [UIColor clearColor];
    NSString *guoJiaMing = [array[indexPath.row] valueForKey:@"cnname"];
    guoJia.text = guoJiaMing;
    guoJia.font = [UIFont systemFontOfSize:20];
    // （重点） 将图片添加到图片上 避免重叠且比cell上的速度更快
    [image addSubview:guoJia];
    
    UILabel *yingWenMing = [[UILabel alloc] initWithFrame:CGRectMake(guoJia.left, guoJia.bottom  - 5, 100, 10)];
    yingWenMing.text = [array[indexPath.row] valueForKey:@"enname"];
    yingWenMing.font = [UIFont systemFontOfSize:14];
    yingWenMing.textColor = [UIColor whiteColor];
    [image addSubview:yingWenMing];
    
    // 黑色背景
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(image.right - 60, 10, 50, 50)];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.7;
    [image addSubview:view];
    
    UILabel *chengShi = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 50, 20)];
    chengShi.text = @"城市";
    chengShi.textAlignment = NSTextAlignmentCenter;
    chengShi.textColor = [UIColor whiteColor];
    [view addSubview:chengShi];
    
    UILabel *chengShiGeShu = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    chengShiGeShu.textColor = [UIColor whiteColor];
    chengShiGeShu.textAlignment = NSTextAlignmentCenter;
    chengShiGeShu.font = [UIFont systemFontOfSize:20];
    
    // 坑点 可能是NSNumber类型
    chengShiGeShu.text = [NSString stringWithFormat:@"%@",[array[indexPath.row] valueForKey:@"count"]];
    
    [view addSubview:chengShiGeShu];
    
    return _cell;
}

// 返回分区数 根tableview一样 默认就一个分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// 设置表头表尾 同代理方法 来实现
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    // 判断返回表头 还是表尾
    // 因为参数是 字符串的 判断相同 不能等号
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        // 返回表头 需要去复用集合中得到
        _tou = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader" forIndexPath:indexPath];
        // 加个颜色
        //        _tou.backgroundColor = [UIColor greenColor];
        
        [_tou addSubview:_imageView];
        [_tou addSubview:_lable];
        return _tou;
    } else {
        
        // 返回表尾
        _wei = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFoot" forIndexPath:indexPath];
        //                _wei.backgroundColor = [UIColor yellowColor];
        // 在表尾上添加 tableView
        [_wei addSubview:_tableView];
        // 在表尾上添加 标题
        [_wei addSubview:_biaoWei];
        // 在表尾添加 标题尾语
        UILabel *pinYin = [[UILabel alloc] initWithFrame:CGRectMake(Kwidth * 0.7, _biaoWei.top, Kwidth - _biaoWei.right, _biaoWei.height)];
        pinYin.text = @"拼音首字母排序";
        pinYin.font = [UIFont systemFontOfSize:15];
        pinYin.alpha = 0.8;
        pinYin.textColor = [UIColor grayColor];
        [_wei addSubview:pinYin];
        return _wei;
    }
}
// 表示图点击
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GuoJiaViewController *guoJiaVC = [[GuoJiaViewController alloc] init];
    // 提取国家id
    guoJiaVC.hidesBottomBarWhenPushed = YES;
    NSArray *array = [_zhouArray valueForKey:@"hot_country"];
    guoJiaVC.ids = [array[indexPath.row] valueForKey:@"id"];
    [self.navigationController pushViewController:guoJiaVC animated:YES];
}

// 添加tableView
- (void)addUITableView
{
    NSArray *array = [_zhouArray valueForKey:@"country"];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, Kwidth, array.count * 50) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor redColor];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = [_zhouArray valueForKey:@"country"];
    if (array.count == 0) {
        _layout.footerReferenceSize = CGSizeMake(0, 0);
        _collectionView.collectionViewLayout = _layout;
    } else {
        _layout.footerReferenceSize = CGSizeMake(0, array.count * 50 + 500);
        _collectionView.collectionViewLayout = _layout;
    }
    
    CGRect table = CGRectMake(0, 30, Kwidth, array.count * 50);
    _tableView.frame = table;
    
    
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string = @"MyCell";
    MuDiDiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        cell = [[MuDiDiTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    NSArray *array = [_zhouArray valueForKey:@"country"];
    cell.zongWen.text = [array[indexPath.row] valueForKey:@"cnname"];
    cell.yingWen.text = [array[indexPath.row] valueForKey:@"enname"];
    cell.yingWen.font = [UIFont systemFontOfSize:14];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GuoJiaViewController *guoJiaVC = [[GuoJiaViewController alloc] init];
    // 提取国家id
    guoJiaVC.hidesBottomBarWhenPushed = YES;
    NSArray *array = [_zhouArray valueForKey:@"country"];
    guoJiaVC.ids = [array[indexPath.row] valueForKey:@"id"];
    [self.navigationController pushViewController:guoJiaVC animated:YES];
    
}


@end
